
/**
 * @fileoverview Entry point for the sample application.
 * @author johannes.nel@gmail.com (Johan)
 * Date: 2012/03/23
 * Time: 11:49 AM
 */

/**
 * @preserve
 *             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
 *                   Version 2, December 2004.
 *
 * Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
 *
 * Everyone is permitted to copy and distribute verbatim or modified
 * copies of this license document, and changing it is allowed as long
 * as the name is changed.
 *
 *            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
 *   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
 *
 *  0. You just DO WHAT THE FUCK YOU WANT TO.
 */

goog.provide('lfpug.Application');
goog.require('goog.net.EventType');
goog.require('goog.net.XhrManager');
goog.require('lfpug.builder.StoryBuilder');
goog.require('lfpug.model.ArrayCollection');
goog.require('lfpug.parser.StoryParser');
goog.require('lfpug.ui.component.ContentUI');
goog.require('lfpug.ui.component.StoriesComponent');
goog.require('lfpug.ui.component.StoryComponent');
goog.require('lfpug.ui.component.TaskComponent');



/**
 * @constructor
 */
lfpug.Application = function() {
  this.init();
  this.setupModel();
  this.setupViewContainer();

};


/**
 * @private
 * @type {lfpug.ui.component.ContentUI}
 */
lfpug.Application.prototype.viewContainer_;


/**
 * setup the xhr manager and the poller associated with it
 */
lfpug.Application.prototype.init = function() {
  this.onDataLoadFunc_ = goog.bind(this.dataLoaded_, this);
  /**
   * @private
   */
  this.xhr_ = new goog.net.XhrManager();
  this.xhr_.addEventListener(goog.net.EventType.COMPLETE, this.onDataLoadFunc_);
  //setup polling
  this.pollerFunc_ = goog.bind(this.pollData_, this);
  setInterval(this.pollerFunc_, 1000);
  this.pollData_();
};


/**
 * Sets up the parser, builder and stories collection
 */
lfpug.Application.prototype.setupModel = function() {
  this.stories_ = new lfpug.model.ArrayCollection();
  this.builder_ = new lfpug.builder.StoryBuilder(this.stories_);
  this.parser_ = new lfpug.parser.StoryParser(this.builder_);
};


/**
 * creates the containing view component.
 */
lfpug.Application.prototype.setupViewContainer = function() {
  this.viewContainer_ = new lfpug.ui.component.ContentUI(this);
  var element = goog.dom.getElement('main');
  this.viewContainer_.decorate(element);
  this.storiesComponent_ = new lfpug.ui.component.StoriesComponent(
      this.stories_
      );
  this.viewContainer_.addChild(this.storiesComponent_, true);
};


/**
 * @private
 */
lfpug.Application.prototype.pollData_ = function() {
  this.xhr_.send('1', '../../../data/stories.json', 'GET');
};


/**
 * @private
 * A very limited implemention of a poller with xhrmanager.
 * @param {goog.events.Event} evt The complete event.
 */
lfpug.Application.prototype.dataLoaded_ = function(evt) {
  var json = evt.xhrIo.getResponseJson();
  this.parser_.parse(json);
};



/*
TODO Discuss making properties and functions availible for external use when
  compiled in advanced mode. A good example of this is an application entry
  point.
*/
//goog.addSingletonGetter(lfpug.Application);
//Document['startApplication'] = lfpug.Application.getInstance;

/**
 * the prefered way to make a function externally visible.
 */
//goog.exportSymbol('application.start', function() {
//  new lfpug.Application();
//});


/**
 * I can even expose the constructor like this
 */
goog.exportSymbol('application.Constructor', lfpug.Application);

/**
 * TODO get detailed difference between exportProp and
 *
 * Notice even after construction the Document.HAS_STARTED remains false.
 */
//goog.exportProperty(Document, 'Application', lfpug.Application.getInstance());
